package com.risetechnology.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.risetechnology.data.Ingredient;
import com.risetechnology.krystalkitchen1.R;

public class IngredientListAdapter extends ArrayAdapter<Ingredient> {

	private Context context;
	private int layoutId;
	private List<Ingredient> data = null;

	public IngredientListAdapter(Context context, int layoutId, List<Ingredient> data) {
		super(context, layoutId, data);
		this.context = context;
		this.layoutId = layoutId;
		this.data = data;
	}

	public View getView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layoutId, parent, false);

			holder = new ViewHolder();

			holder.textviewIngredient = (TextView) convertView.findViewById(R.id.tv_ingredient);
			holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		String ingredient = data.get(position).getQuantity() + " "
				+ data.get(position).getUnit() + " "
				+ data.get(position).getName() + " "
				+ data.get(position).getDescription();
		holder.textviewIngredient.setText(ingredient);
		
		holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				data.get(position).setSelected(isChecked);
			}
		});
		
		holder.checkbox.setChecked(data.get(position).isSelected());
		
		convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView textviewIngredient;
		CheckBox checkbox;
	}

}
